package com.ah.hf.service.impl;

import com.ah.hf.dto.DishDto;
import com.ah.hf.entity.Dish;
import com.ah.hf.entity.DishFlavor;
import com.ah.hf.mapper.DishMapper;
import com.ah.hf.service.DishFlavorService;
import com.ah.hf.service.DishService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jdk.nashorn.internal.ir.CallNode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper , Dish>
        implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Override
    @Transactional
    public void saveDish(DishDto dishDto) {
        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);

        //菜品id
        Long dishId = dishDto.getId();

        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        /*flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);*/

        //可以使用下面方法，跟上面一样
        for (DishFlavor dishFlavor : flavors){
            dishFlavor.setDishId(dishId);
            dishFlavorService.save(dishFlavor);
        }
    }

    //根据菜品菜品id查询菜品信息和菜品口味数据
    @Override
    public DishDto getByIDWithFlavor(Long id) {
        //根据id查询菜品信息
        Dish dish = this.getById(id);
        //获取dishID
        Long dishId = dish.getId();
        DishDto dishDto = new DishDto();

        //调价构造器
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId,dishId);

        //拷贝数据
        BeanUtils.copyProperties(dish,dishDto);

        //查出对应的口味
        List<DishFlavor> dishFlavors = dishFlavorService.list(qw);

        //把数据赋给Flavors List集合中
        dishDto.setFlavors(dishFlavors);
        return dishDto;
    }

    @Override
    @Transactional//操作两张表使用事务注解，保证一致性
    public void updateWithFlavor(DishDto dishDto) {
        //跟新dish标的基本信息
        this.updateById(dishDto);
        //清空当前对应口味的数据
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(qw);

        //获取DishDto list集合
        List<DishFlavor> dishFlavors = dishDto.getFlavors();


        for (DishFlavor dishFlavor : dishFlavors){
            dishFlavor.setDishId(dishDto.getId());
            //更新操作
            dishFlavorService.save(dishFlavor);
        }

        /*dishFlavors = dishFlavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(dishFlavors);*/
    }
}
